package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client_logic.DealSale;
import Client_logic.Template;
import Client_logic.ConnectionToServer;
import java.awt.Font;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.SystemColor;

/**
 * This class use to search for template.
 * @author matan
 * @author sharon
 * @author michael
 * @author ido
 */
public class TemplateSearch extends JFrame
{
	
	private JComboBox cIDComboBox = null;
	private JLabel cIDlLabel = null;
	private JButton searchButton = null;
	private JPanel jContentPane = null;
	private TemplateForm templateForm = null;
	private Template template = null;
	private DealSaleForm dealsaleForm = null;
	private long cID = 0;
	private String initialitoForm;
	private DealSale dealsale = null;
	private ScheduleForm scheduleForm = null;
	
	/**
	 * This is the default constructor
	 */
	public TemplateSearch(String initialitoForm) 
	{
		super();
		setIconImage(Toolkit.getDefaultToolkit().getImage(TemplateSearch.class.getResource("/javax/swing/plaf/metal/icons/ocean/collapsed-rtl.gif")));
		this.initialitoForm = initialitoForm;
		initialize();		
	}
	
	/**
	 * This method initializes ProductForm
	 */
	private void initialize() 
	{
		template = new Template(0, "", null, "", "");
		dealsale = new DealSale(0,null, null, null, template);
		dealsaleForm = new DealSaleForm();
		scheduleForm = new ScheduleForm(dealsale);
		this.setContentPane(getJContentPane());
		this.setTitle("Template Search");
	}
	
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setForeground(Color.BLACK);
			jContentPane.setLayout(null);
			
			cIDlLabel = new JLabel();
			cIDlLabel.setBackground(Color.RED);
			cIDlLabel.setBounds(new Rectangle(85, 73, 113, 22));
			cIDlLabel.setText("Template number:");
							
			jContentPane.setLayout(null);
			jContentPane.add(cIDlLabel, null);
			jContentPane.add(getCIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			templateForm = new TemplateForm();
			
			templateForm.getBackjButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					setContentPane(jContentPane);								
				}
			});
			
			templateForm.getBackjButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					setContentPane(jContentPane);								
				}
			});
			
			dealsaleForm.getNextjButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (dealsaleForm.ifNext())
					{
						scheduleForm.setDealSale(dealsaleForm.getDealSale());
						scheduleForm.updateClientsjComboBox();
						scheduleForm.updateSelsmenjComboBox();
						setSize(550, 420);
						setContentPane(scheduleForm);
					}	
				}	
			});
		}
		return jContentPane;		
	}
	
	/**
	 * This method initializes PIDComboBox	
	 */
	private JComboBox getCIDComboBox()
	{
		if(cIDComboBox == null)
		{	
			cIDComboBox = new JComboBox();
			cIDComboBox.setForeground(Color.RED);
			cIDComboBox.setBounds(new Rectangle(234, 72, 127, 25));
			long[] templatesID = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				templatesID = (long[]) connectiontoserver.loadAllTemplates();
				if (templatesID == null)
				{
					JOptionPane.showMessageDialog(null, "Templates is EMPTY!!!");
					cIDComboBox.addItem(0);
				}	
				else
				{
					int len = templatesID.length;
					if (len > 0)
						cID = templatesID[0];
					for(int i=0; i < len; i++)
					{
						cIDComboBox.addItem(templatesID[i]);
					}	
				}
			}
			catch(Exception e)
			{
				cIDComboBox.addItem(0);
			}
			cIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					cID = (Long)cIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return cIDComboBox;
	}

	
	/**
	 * This method initializes searchButton	
	 */
	private JButton getSearchButton() 
	{
		if (searchButton == null) 
		{
			searchButton = new JButton();
			searchButton.setForeground(Color.BLACK);
			searchButton.setBackground(Color.RED);
			searchButton.setFont(new Font("David", Font.BOLD, 14));
			searchButton.setBounds(new Rectangle(154, 136, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (initialitoForm.equals("UpdateTemplate"))
						initialiToUpdateTemplateForm();	
					if (initialitoForm.equals("DealSale"))
						initialiToActiveTemplateForm();	
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This method initializes to UpdateTemplateForm
	 */
	private void initialiToUpdateTemplateForm()
	{		
		try 
		{
			if (cID > 0)
			{
				if (template.loadTemplate(cID))
				{
					templateForm.loadTemplate(template);
					templateForm.addUpdateButton();
					this.setTitle("CAMPAIGN UPDATE");
					setContentPane(templateForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a template");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
	
	/**
	 * This method initializes to ActiveTemplateForm
	 */
	private void initialiToActiveTemplateForm()
	{		
		try 
		{
			if (cID > 0)
			{
				if (template.loadTemplate(cID))
				{
					dealsale.setTemplate(template);
					dealsaleForm.loadDealSale(dealsale);
					this.setTitle("ACTIVE CAMPAIGN");
					dealsaleForm.addNextjButton();
					setContentPane(dealsaleForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a template");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
	
}
